<?php

namespace App\Controllers;

use Framework\Database;
use Framework\Validation;
use Framework\Session;

class UserController{
    protected $db;

    public function __construct()
    {
        $config = require basePath('config/db.php');
        $this->db = new Database($config);
    }

    public function login()
    {
        loadView('users/login');
    }

    public function create()
    {
        loadView('users/create');
    }

    public function store()
    {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $city = $_POST['city'];
        $province = $_POST['province'];
        $password = $_POST['password'];
        $passwordConfirmation = $_POST['password_confirmation'];

        $errors = [];

        if(!Validation::email($email)){
            $errors['email'] = '请输入一个合法的电子邮箱地址';
        }

        if(!Validation::string($name,2,50)){
            $errors['name'] = '名字长度为2到50!';
        }
        if(!Validation::string($password,6,50)){
            $errors['password'] = '密码长度至少6位!';
        }
        if(!Validation::match($password,$passwordConfirmation)){
            $errors['password_confirmation'] = '两次的输入密码不一致!';
        }

        if(!empty($errors)){
            loadView('users/create',[
                'errors'=>$errors,
                'uesr'=>[
                'name'=>$name,
                'email'=>$email,
                'city'=>$city,
                'province'=>$province,    
                ]
                ]);
                exit;
        }else{
           $params = [
            'email' => $email
           ];
           $user = $this->db->query('SELECT * FROM users WHERE email = :email',$params)->fetch();
           if($user){
            $errors['email'] = '该邮箱已注册!';
            loadView('users/create',[
                'errors'=>$errors
            ]);
            exit;
           }

           $params = [
            'name'=>$name,
            'email'=>$email,
            'city'=>$city,
            'province'=>$province,
            'password'=>password_hash($password, PASSWORD_DEFAULT)
           ];

           $this->db->query('INSERT INTO users (name,email,city,province,password) VALUES (:name,:email,:city,:province,:password)',$params);

           $userId= $this->db->conn->lastInsertId();

           Session::set('user',[
            'id'=>$userId,
            'name'=>$name,
            'email'=>$email,
            'city'=>$city,
            'province'=>$province
           ]);
           redirect('/');
        }

    }


    public function logout()
    {
        Session::clearAll();
        $params = session_get_cookie_params();
        setcookie('PHPSESSID','',time()-86400,$params['path'],$params['domain']);
        redirect('/');
    }


    public function authenticate()
    {
        $email = $_POST['email'];
        $password = $_POST['password'];

        $errors = [];

        if(!Validation::string($password,6,50)){
            $errors['password'] = '密码为至少6位!';
        }
        
        if(!empty($errors)){
            loadView('users/login',[
                'errors'=>$errors
            ]);
            exit;
        }

        $params = [
            'email' => $email
        ];

        $user = $this->db->query('SELECT * FROM users WHERE email = :email',$params)->fetch();

        if (!$user){
            $errors['email'] = '用户不存在或密码错误!';
            loadView('users/login',[
                'errors'=>$errors
            ]);
            exit;
        }

        Session::set('user',[
            'id'=>$user->id,
            'name'=>$user->name,
            'email'=>$user->email,
            'city'=>$user->city,
            'province'=>$user->province
        ]);
        redirect('/');
    }
}